import json
from datetime import datetime

class DatetimeEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.isoformat()
        return json.JSONEncoder.default(self, obj)

now = datetime.now()
json_str = json.dumps({"timestamp": now}, cls=DatetimeEncoder)
print(json_str)